package com.cocola.controller;

import com.cocola.pojo.Education;
import com.cocola.service.EducationSercice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

import static com.cocola.controller.UserController.USERNAME;

@Controller
@CrossOrigin
public class EducationController {


    @Autowired
    private EducationSercice educationSercice;


    @RequestMapping("/toEducation")
    public String toEducation(Model model, HttpSession session) {

        System.out.println("userName:" + USERNAME);
        try {
            if(USERNAME != null) {
                List<Education> educationList = educationSercice.findEducation();
                model.addAttribute("educationList", educationList);
                return "apply5";

            }else {
                model.addAttribute("error", "请登陆");
                return "login_person";
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return "main";
    }


    @RequestMapping("Education")
    public String education(Model model) {
        if(USERNAME != null) {
            return "apply6";

        } else {
            model.addAttribute("error", "请登陆");
            return "login_person";
        }
    }


    @RequestMapping("/doEducation")
    public String doEducation(String graduationDate, String educationNum, String educationLeval,
                              String educationSchool, String educationType, String educationMajor,
                              String educationDegree, String certificateNumber) {

        try {
            educationSercice.add(graduationDate, educationNum, educationLeval,
                    educationSchool, educationType, educationMajor,
                    educationDegree, certificateNumber, USERNAME);

            return "redirect:/toDate";

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "apply5";

    }

    @RequestMapping("/updateEducation")
    public String updateEducation(String graduationDate, String educationNum, String educationLeval,
                                String educationSchool, String educationType, String educationMajor,
                                String educationDegree, String certificateNumber) {

        educationSercice.update(graduationDate, educationNum, educationLeval,
                educationSchool, educationType, educationMajor,
                educationDegree, certificateNumber, USERNAME);
        return "redirect:/toEducation";

    }

    @RequestMapping("/deleteEducation/{certificateNumber}")
    public String deleteEducation(@PathVariable String certificateNumber, Model model) {

        try {
            educationSercice.delete(certificateNumber);
            System.out.println("Delete Education done ====================================>");
            return "redirect:/toEducation";

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/toEducation";
    }
}
